<template>
  <el-dialog title="新增部门" :visible="isShowDialog" @close="btnCancel">
    <el-form ref="addForm" label-width="120px" :model="formData" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="formData.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="formData.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="formData.manager" style="width:80%" placeholder="请选择">
          <el-option v-for="user in employeeList" :key="user.id" :value="user.username" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="formData.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>

    </el-form>
    <el-row slot="footer" type="flex" justify="center">
      <!-- 列被分为24 -->
      <el-col :span="6">
        <el-button type="primary" size="small" @click="btnOK">确定</el-button>
        <el-button size="small" @click="btnCancel">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getEmployeeSimple } from '@/api/employees.js'
import { addDepartments } from '@/api/departments.js'
import { getDepartDetail } from '@/api/departments.js'
import { editDepts } from '@/api/departments.js'

export default {

  props: {
    isShowDialog: {
      type: Boolean,
      required: false
    },
    nodeData: {
      type: Object,
      required: true
    }

  },
  data() {
    return {
      formData: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: 'blur' }

        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门介绍要求1-50个字符', trigger: 'blur' }

        ]
      },
      employeeList: []
    }
  },
  async created() {
    const res = await getEmployeeSimple()
    this.employeeList = res.data
    console.log(res.data)
  },
  methods: {
    btnOK() {
      this.$refs.addForm.validate(async isOk => {
        if (isOk) {
          if (this.formData.id) {
            await editDepts(this.formData)
            this.$message.success('编辑成功')
          } else {
            await addDepartments({ ...this.formData, pid: this.nodeData.id })
            this.$message.success('添加成功')
          }

          this.$emit('reloadPage')
          this.$emit('closeDialog')
        }
      })
    },
    btnCancel() {
      this.$emit('closeDialog')
      this.formData = {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      }
      this.$refs.addForm.resetFields()
    },
    async  getDepartDetail(id) {
      const res = await getDepartDetail(id)
      this.formData = res.data
    }
  }

}
</script>

<style>

</style>
